using System;
using System.Data;
using System.Data.Common;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;

namespace Tilver.DBConnectionUI.Firebird
{
    [ToolboxItem(false)]
    internal partial class FirebirdEditor : UserControl, IUDLEditor
    {
        Firebird.FirebirdStringBuilder _builder = new FirebirdStringBuilder();
        public FirebirdEditor()
        {
            InitializeComponent();

            this.ckUseServerMode.CheckedChanged += delegate { this.setServerMode(); };
            this.txServer.Validated += delegate { _builder.DataSource = this.txServer.Text; };
            this.txDatabase.Validated += delegate { _builder.Database = this.txDatabase.Text; };
            this.txUserID.Validated += delegate { _builder.User = this.txUserID.Text; };
            this.txPassword.Validated += delegate { _builder.Password = this.txPassword.Text; };
            this.txRole.Validated += delegate { _builder.Role = txRole.Text; };
            this.cmdFindDatabase.Click += delegate { this.FindFileDatabase(); };

            ckUseServerMode.Checked = true;
        }

        #region IConnectionEditor Members

        public string ConnectionString
        {
            get
            {
                return _builder.ConnectionString;
            }
            set
            {
                _builder.ConnectionString = value;
                this.RefreshData();
            }
        }

        public void RefreshData()
        {
            if (_builder.ServerType == 1)
                this.ckUseEmbeddedMode.Checked = true;
            else
                this.ckUseServerMode.Checked = true;

            this.txServer.Text = _builder.DataSource;
            this.txPort.Text = _builder.Port.ToString();
            this.txDatabase.Text = _builder.Database;
            this.txUserID.Text = _builder.User;
            this.txPassword.Text = _builder.Password;
            this.txRole.Text = _builder.Role;
        }

        public object GetAdvancedPropertiesObject()
        {
            return _builder;
        }

        #endregion

        #region Methods

        private void setServerMode()
        {
            bool isServerDb = ckUseServerMode.Checked;
            if (isServerDb)
            {
                _builder.ServerType = 0;
            }
            else
            {
                _builder.ServerType = 1;
                txServer.Text = "";
                txDatabase.Text = "";
                txPort.Text = "3050";
                txUserID.Text = "";
                txPassword.Text = "";
                txRole.Text = "";
            }

            txServer.Enabled = isServerDb;
            txPort.Enabled = isServerDb;
            txUserID.Enabled = isServerDb;
            txPassword.Enabled = isServerDb;
            txRole.Enabled = isServerDb;
            cmdFindDatabase.Enabled = !isServerDb;
        }

        private void FindFileDatabase()
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "Firebird Databases *.fdb|*.fdb|All Files|*.*";
            if (fd.ShowDialog(this.Parent) == DialogResult.OK)
            {
                this.txDatabase.Text = fd.FileName;
                _builder.DataSource = fd.FileName;
            }
        }

        #endregion

    }
}
